{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018/12/32\n",
      "2018/12/31\n"
     ]
    }
   ],
   "source": [
    "class Date:\n",
    "    def __init__(self, year, month, day):\n",
    "        self.year = year\n",
    "        self.month = month\n",
    "        self.day = day\n",
    "    \n",
    "    def tomorrow(self):\n",
    "        self.day += 1\n",
    "\n",
    "    # 静态方法\n",
    "    @staticmethod\n",
    "    def parse_from_string(date_str):\n",
    "        year, month, day = tuple(date_str.split('-'))\n",
    "        return Date(int(year), int(month), int(day))\n",
    "    \n",
    "    # 实例方法\n",
    "    @classmethod\n",
    "    def from_string(cls, date_str):\n",
    "        year, month, day = tuple(date_str.split('-'))\n",
    "        return cls(int(year), int(month), int(day))\n",
    "    \n",
    "    def __str__(self):\n",
    "        return \"{year}/{month}/{day}\".format(year=self.year, month=self.month, day=self.day)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    new_day = Date(2018, 12, 31)\n",
    "    new_day.tomorrow()\n",
    "    print(new_day)\n",
    "    \n",
    "    # 2018-12-31\n",
    "    date_str = '2018-12-31'\n",
    "    \n",
    "#     year, month, day = tuple(date_str.split('-'))\n",
    "#     new_day = Date(int(year), int(month), int(day))\n",
    "#     print(new_day)\n",
    "    \n",
    "    # staticmethod 完成操作\n",
    "#     new_day = Date.parse_from_string(date_str)\n",
    "#     print(new_day)\n",
    "    \n",
    "    # classmethod 完成操作\n",
    "    new_day = Date.from_string(date_str)\n",
    "    print(new_day)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
